{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  刘入铭\n",
    "+ 学号  0151819"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Today I learnt a new song, \n",
      "abcdefghijklmnopqrstuvwxyz.\n",
      "let's sing it again. \n",
      "abcdefghijklmnopqrstuvwxyz.\n",
      "the song is over. \n"
     ]
    }
   ],
   "source": [
    "def fuge():\n",
    "    print(\"abcdefghijklmnopqrstuvwxyz.\")\n",
    "    \n",
    "print(\"Today I learnt a new song, \")\n",
    "fuge()\n",
    "print(\"let's sing it again. \")\n",
    "fuge()\n",
    "print(\"the song is over. \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.5\n",
      "8.25\n",
      "9\n",
      "\n",
      "\n",
      "-5.5\n",
      "8.25\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "def mean(datas):\n",
    "\tnumber = 0\n",
    "\tsummary = 0\n",
    "\tfor data in datas:\n",
    "\t\tsummary += data\n",
    "\t\tnumber += 1\n",
    "\n",
    "\tmean_value = summary/number\n",
    "\tprint(mean_value)\n",
    "\n",
    "def variance(datas):\n",
    "\tsummary = 0\n",
    "\tsum = 0\n",
    "\tnumber = 0\n",
    "\tfor data in datas:\n",
    "\t\tsummary += data\n",
    "\t\tnumber += 1\n",
    "\tmean_value = summary/number\n",
    "\n",
    "\tfor data in datas:\n",
    "\t\tsum += (data - mean_value)**2\n",
    "\t\n",
    "\tvariance_value = sum/number\n",
    "\tprint(variance_value)\n",
    "    \n",
    "def range(datas):\n",
    "    max = datas[0]\n",
    "    min = datas[0]\n",
    "    for data in datas:\n",
    "        if data >= max:\n",
    "            max = data\n",
    "        if data <= min:\n",
    "            min = data\n",
    "    range_value = max - min\n",
    "    print(range_value)\n",
    "    \n",
    "def total(datas):\n",
    "    mean(datas)\n",
    "    variance(datas)\n",
    "    range(datas)\n",
    "    \n",
    "data1 = [1, 2, 3, 4 , 5, 6, 7, 8, 9, 10]\n",
    "data2 = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]\n",
    "\n",
    "total(data1)\n",
    "print(\"\\n\")\n",
    "total(data2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "1.25\n",
      "3\n",
      "\n",
      "\n",
      "2.5\n",
      "1.25\n",
      "3\n",
      "1.118033988749895\n"
     ]
    }
   ],
   "source": [
    "def mean(datas):\n",
    "\tnumber = 0\n",
    "\tsummary = 0\n",
    "\tfor data in datas:\n",
    "\t\tsummary += data\n",
    "\t\tnumber += 1\n",
    "\n",
    "\tmean_value = summary/number\n",
    "\tprint(mean_value)\n",
    "\n",
    "def variance(datas):\n",
    "\tsummary = 0\n",
    "\tsum = 0\n",
    "\tnumber = 0\n",
    "\tfor data in datas:\n",
    "\t\tsummary += data\n",
    "\t\tnumber += 1\n",
    "\tmean_value = summary/number\n",
    "\n",
    "\tfor data in datas:\n",
    "\t\tsum += (data - mean_value)**2\n",
    "\t\n",
    "\tvariance_value = sum/number\n",
    "\tprint(variance_value)\n",
    "    \n",
    "def range(datas):\n",
    "    max = datas[0]\n",
    "    min = datas[0]\n",
    "    for data in datas:\n",
    "        if data >= max:\n",
    "            max = data\n",
    "        if data <= min:\n",
    "            min = data\n",
    "    range_value = max - min\n",
    "    print(range_value)\n",
    "    \n",
    "#如果保持sd=0,则不打印样本标准差\n",
    "def total(datas, sd = 0):\n",
    "    mean(datas)\n",
    "\n",
    "    summary = 0\n",
    "    sum = 0\n",
    "    number = 0\n",
    "    for data in datas:\n",
    "        summary += data\n",
    "        number += 1\n",
    "    mean_value = summary/number\n",
    "\n",
    "    for data in datas:\n",
    "        sum += (data - mean_value)**2\n",
    "    variance_value = sum/number\n",
    "    print(variance_value)\n",
    "    \n",
    "    range(datas)\n",
    "    \n",
    "    if sd != 0:\n",
    "        standard_deviation = pow(variance_value, 1/2)\n",
    "        print(standard_deviation)\n",
    "        \n",
    "total([1,2,3,4])\n",
    "print(\"\\n\")\n",
    "total([1,2,3,4], 2)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5\n",
      "1.25\n",
      "3\n",
      "\n",
      "\n",
      "2.5\n",
      "1.25\n",
      "3\n",
      "1.118033988749895\n",
      "4\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "def total(datas, sd=0, maxi=0, mini=0):\n",
    "    summary = 0\n",
    "    sum = 0\n",
    "    number = 0\n",
    "    max = datas[1]\n",
    "    min = datas[1]\n",
    "    \n",
    "    for data in datas:\n",
    "        summary += data\n",
    "        number += 1\n",
    "    mean_value = summary/number\n",
    "    \n",
    "    for data in datas:\n",
    "        sum += (data - mean_value)**2\n",
    "    variance_value = sum/number\n",
    "    \n",
    "    for data in datas:\n",
    "        if data >= max:\n",
    "            max = data\n",
    "        if data <= min:\n",
    "            min = data \n",
    "    range_value = max - min\n",
    "    \n",
    "    print(mean_value)\n",
    "    print(variance_value)\n",
    "    print(range_value)\n",
    "    \n",
    "    if sd != 0:\n",
    "        standard_deviation = pow(variance_value, 1/2)\n",
    "        print(standard_deviation)\n",
    "    if maxi != 0:\n",
    "        print(max)\n",
    "    if mini != 0:\n",
    "        print(min)\n",
    "\n",
    "total([1,2,3,4], 0,0,0)\n",
    "print(\"\\n\")\n",
    "total([1,2,3,4], 1, 1, 1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
